#include "exifTagParser.h"


int getEXIFInfo(char *filename,exiffiledata *exifdata)
{
	int i;
	char tempBuff[1024];
	ExifEntry *entry;
	ExifData *data = exif_data_new_from_file(filename);

	if (data == NULL)
	{
		printf("\nERROR: Cannot read EXIF data from file: %s",filename);
		return -1;
	}
	exifdata->filename = strdup(filename);
	for (i = 0; i < 6; i++)
	{
		memset(tempBuff,0,1024);
		if (i == 0)
		{
			entry = exif_content_get_entry(data->ifd[EXIF_IFD_0],(ExifTag) MODEL);
			if (entry != NULL)
			{
				exif_entry_get_value(entry,tempBuff,sizeof(tempBuff));
				exifdata->model = strdup(tempBuff);
			}
			else
			{
				exifdata->model = strdup("Unknown");	
			}
		}
		else if (i == 1)
		{
			entry = exif_content_get_entry(data->ifd[EXIF_IFD_0],(ExifTag) MANUFACTURER);
			if (entry != NULL)
			{
				exif_entry_get_value(entry,tempBuff,sizeof(tempBuff));
				exifdata->manufacturer = strdup(tempBuff);
			}
			else
			{
				exifdata->manufacturer = strdup("Unknown");	
			}
		}
		else if (i == 2)
		{
			entry = exif_content_get_entry(data->ifd[EXIF_IFD_0],(ExifTag) COLORSPACE);
			if (entry != NULL)
			{
				exif_entry_get_value(entry,tempBuff,sizeof(tempBuff));
				exifdata->colorspace = strdup(tempBuff);
			}
			else
			{
				exifdata->colorspace = strdup("Unknown");	
			}
		}
		else if (i == 4)
		{
			exifdata->path = strdup(filename);
		}
		else if (i == 5)
		{
			char *temp = strrchr(filename,'/');
			exifdata->filename = strdup(temp+1);
		}
	}
}
void getEXIFInfostr(char *filename,char *fileInfo)
{
	//TODO
}
void printEXIFData(exiffiledata *exifdata)
{
	if (exifdata != NULL)
	{
		if (exifdata->filename != NULL)
		{
			printf("\n%s: ",exifdata->filename);
		}
		if (exifdata->model != NULL)
		{
			printf("%s ",exifdata->model);
		}
		if (exifdata->manufacturer != NULL)
		{
			printf("%s ",exifdata->manufacturer);
		}
		if (exifdata->colorspace != NULL)
		{
			printf("%s ",exifdata->colorspace);
		}
	}
}

void initEXIFStruct(exiffiledata *exifdata)
{
	exifdata->filename = NULL;
	exifdata->manufacturer = NULL;
	exifdata->model = NULL;
	exifdata->colorspace = NULL;
}

void freeEXIFStruct(exiffiledata *exifdata)
{
	free(exifdata->filename);
	free(exifdata->manufacturer);
	free(exifdata->model);
	free(exifdata->colorspace);
}

